package com.thinkit.bigdata.core.util.audios;

import org.apache.log4j.Logger;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * 音频處理工具類
 * Created by hccl on 2018/2/27.
 */
public class AudioUtil {

    private static Logger logger = Logger.getLogger(AudioUtil.class);

    /**
     * 獲取音頻採樣率
     * @param file -
     * @return -
     */
    public static String getAudioSampleSizeInBit(File file) {
        logger.info(file.getAbsolutePath());
        AudioInputStream ais = null;
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        try {
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            ais = AudioSystem.getAudioInputStream(bis);
            AudioFormat format = ais.getFormat();
            return format.getSampleSizeInBits() + "";
        } catch (Exception e) {
            logger.info("【音频采样率校验方法】不是标准格式音频");
            return null;
        } finally {
            try {
                if (ais != null) {
                    ais.close();
                }
                if (bis != null) {
                    bis.close();
                }
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                logger.error("【音频采样率校验方法】流关闭异常");
            }

        }
    }

    public static void main(String[] args) throws IOException {

        File file_8 = new File("C:/Users/hccl/Desktop/jiakia_license/8k8bit.wav");
        File file_16 = new File("C:/Users/hccl/Desktop/jiakia_license/8k16bit.wav");
        try {
            AudioInputStream ais_8 = AudioSystem.getAudioInputStream(file_8);
            AudioFormat format_8 = ais_8.getFormat();
            System.out.println(format_8.getChannels());
            System.out.println(format_8.getEncoding());
            System.out.println(format_8.getFrameRate());
            System.out.println(format_8.getFrameSize());
            System.out.println(format_8.getSampleRate());
            System.out.println(format_8.getSampleSizeInBits());
            ais_8.close();
            System.out.println("-------------------------------");
            AudioInputStream ais_16 = AudioSystem.getAudioInputStream(file_16);
            AudioFormat format_16 = ais_16.getFormat();
            System.out.println(format_16.getChannels());
            System.out.println(format_16.getEncoding());
            System.out.println(format_16.getFrameRate());
            System.out.println(format_16.getFrameSize());
            System.out.println(format_16.getSampleRate());
            System.out.println(format_16.getSampleSizeInBits());
            ais_16.close();
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        }
    }


}